<?php

class RoomsController extends AppController {
    
    // define controller name
    var $name = 'Rooms';
    
    // define list of models to use 
    var $uses = array('Service','Room');
    
    // set controller domain
    var $controllerDomain = 'rooms';
    
    public function beforeFilter() {
        parent::beforeFilter();
        
    }
    
    public function isAuthorized() {
        
        $valid = parent::isAuthorized();
        if ($valid == false) {
            return false;
        }
        
        switch($this->action) {
            default:
                break;
        }
         
        return true;
    }
    
    
    // create a new user
    public function create() {
        
        // get active room types
        $this->getRoomTypes();
       
        // create new room
        if ($this->request->is('post')){ 
            
            if ($this->Room->sanitizeAll() && $this->Room->validates() && $this->Room->save($this->request->data)) {               
                $this->Session->setFlash(__('Room created',true));  
                $this->redirect(array('controller' => 'rooms','action' => 'index'));
            } 
               
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        }
    }
    
    public function delete($id = null) {
       
        if (!$this->Room->activeRecordExists($id)) {
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'rooms','action' => 'index'));           
        }
        
        $this->Service->delete($id);
        $this->Session->setFlash(__('Room deleted',true));
        $this->redirect(array('controller' => 'rooms','action' => 'index'));
        
    }
    
    public function edit($id = null) {
        
        // chek that record exists
        if (!$this->Room->activeRecordExists($id)) {
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'rooms','action' => 'index'));          
        }
        
        // get db record
        $dbRoom = $this->Room->find('first',array('conditions' => array('id' => $id, 'deletedOn' => null)));
        
        // get active room types
        $this->getRoomTypes();
        
        // set data for modifications
        if ($this->request->is('get')) {
            $this->request->data = $dbRoom;
        }
        
        // process modifications
        if ($this->request->is('post')){ 
            
            $this->Room->create();
            $this->Room->id = $id;
            
            if ($this->Room->sanitizeAll() && $this->Room->validates() && $this->Room->save($this->request->data)) {
                $this->Session->setFlash(__('Update successfull',true));  
                $this->redirect(array('controller' => 'rooms','action' => 'index'));        
            } 
               
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        }
    }
    
    public function view($id = null) {
        //todo
    }
    
    public function index() {
        //todo
    }
    
    public function get_items() {
        //todo
    }
 
    private function getRoomTypes($set = true, $persist = true) {
        
        $roomTypes = array();
        
        $dbRoomTypes = $this->Service->find('all',array('conditions' => array('type' => SERVICE_TYPE_ROOM,'deletedOn' => null)));
        foreach($dbRoomTypes as $type) {
            $roomTypes[$type['Service']['id']] = $type['Service']['name'];
        }
        
        if ($set) $this->roomTypes = $roomTypes;
        if ($persist) $this->set('roomTypes',$roomTypes);
        
        return $roomTypes;
    }
}

?>